<?php
namespace Admin\Controller;
use Think\Controller;
use Common\BaseController;
use Think\Upload;
use Common\Common\Log;
class RoleController extends BaseController {


    //角色信息
    public function role_list(){
        $templData['title']="角色列表";
        $User = M('Role'); // 实例化User对象
        $count      = $User->where('status=1')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->where('status=1')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k=>$v){
            if($v['eName']=="super_admin"){
                $list[$k]['node_name']="超级管理员拥有所有权限！";
            }else{
                $node_id=M("role_node")->where('role_id="%d"',$v['id'])->select();
                if(!empty($node_id)){
                    $str="";
                    foreach($node_id as $key=>$val){
                        $node=M("Node")->where("id='%d'",$val['node_id'])->find();
                        $str.=",".$node['node_name'];
                    }
                    $list[$k]['node_name']=substr($str,1);
                }else{
                    $list[$k]['node_name']="";
                }
            }

        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign($templData);
        $this->display();
    }

    /**
     * 角色修改
     */
    public function edit(){
        $tmplData['title'] = '角色修改'; // 标题

        if(I( 'post.' )){
            if(session('checkDataKey')==$this->createCheckDataKey(I( 'post.' ))){
                // 验证已通过
                $data['role_name'] = I( 'post.role_name' );
                $data['eName'] = I( 'post.eName' );
                $roleId=I('post.role_id');
                $url='role_list';
                if(M( 'Role' )->data($data)->where('id="%d"',$roleId)->save()){
                    $content='角色修改成功';
                    Log::write2($content);
                    $this->showMsg('修改成功！','success',$url);
                }else{
                    $this->showMsg('修改失败！','error',$url);
                }
            }else{
                $this->showMsg('操作异常！','warning','admin/info');
            }
        }else{
            //查询记录
            $roleId=I('get.roleId');
            $re=M('role')->where('id="%d"',$roleId)->find();
            if($re){
                $tmplData['roleId']=$re['id'];
                $tmplData['role_name']=$re['role_name'];
                $tmplData['eName']=$re['eName'];
                $tmplData['status']=$re['status'];
            }else{
                $url='info';
                $this->showMsg('操作异常！','warning',$url);
            }
            $this->assign( $tmplData );
            $this->display('Role_role_edit');
        }
    }
    //删除角色
    public function del()
    {
        $role_id=I("get.roleId");
        $url='role_list';
        if(M("Role")->where('id="%d"',$role_id)->setField('status',0))
        {
            $content='角色删除成功';
            Log::write2($content);
            $this->showMsg('删除成功！','success',$url);
        }else{
            $this->showMsg('删除失败！','error',$url);
        }
    }

    //验证角色唯一性
    public function checkRoleUnique()
    {
        if(I('post.role_name')){
              if(!I("post.id")){
                  $where='role_name="%s"';
              }else{
                  $id=I("post.id");
                  $where='role_name="%s" AND id!='.$id.' ';
              }
            if(M('Role')->where($where,I('post.role_name'))->count()){
                $data=array(
                    'tips'=>'该角色已经存在！',
                );
            }else{
                $data=array(
                    'tips'=>'',
                );
            }
        }else{
            $data=array(
                'tips'=>'系统异常！',
            );
        }
        $this->ajaxReturn($data);
    }

   //添加角色
    public function role_add()
    {
        $tmplData['title'] = '新增角色'; // 标题
        if(I( 'post.' )){
            if(session('checkDataKey')==$this->createCheckDataKey(I( 'post.' ))){
                // 验证已通过
                $data['role_name'] = I( 'post.role_name' );
                $data['eName'] = I( 'post.eName' );
                $data['status'] = I( 'post.status' );
                if(M( 'Role' )->data( $data )->add()){
                    $lastInsId=M('Role')->getLastInsID();
                    $url='role_list';
                    $param='roleId='.$lastInsId;
                    $content='新增角色成功';
                    Log::write2($content);
                    $this->showMsg('添加成功！','success',$url,$param);
                }else{
                    $this->showMsg('添加失败！','error',"role_list");
                }
            }else{
                $this->showMsg('操作异常！','warning');
            }
        }
        $node=M("node")->where("pid!=0 AND status=1")->select();
        $this->assign("node",$node);
        $this->assign( $tmplData );
        $this->display();
    }
    //权限分配
    public function node_set()
    {
        if(I("post.")){
            $List=I('post.');
            $accessData=array();//权限数据
            foreach($List as $k=>$v){
                $role_id=str_replace('node_', '', $k);
                foreach($v as $value){
                    $group_id=$value;
                    $accessData[]=array(
                        'role_id'=>$role_id,
                        'node_id'=>$group_id,
                    );
                }
            }
            //设置权限
                 $flag=false;
                if(M('RoleNode')->where("1")->delete()){
                    $flag=true;
                }
            if($flag){
                if(M('RoleNode')->addAll($accessData)){
                    $content='设置操作权限成功';
                    Log::write2($content);
                    $this->showMsg('设置成功！','success','role_list');
                }else{
                    $this->showMsg('设置失败！','error','role_list');
                }
            }else{
                $this->showMsg('系统异常！','warning','node_set');
            }
        }else{
            $node=M("node")->where("status=1 AND pid!=0")->select();
            $role=M("role")->where("status=1")->select();
            foreach($role as $k=>$v){
                $role_node=M("role_node")->where("role_id=%d",$v['id'])->select();
                if(!empty($role_node)){
                    foreach($role_node as $key=>$val){
                        $role[$k]['r'][]=$val['node_id'];
                    }
                }else{
                    $role[$k]['r'][]="";
                }
            }
            $this->assign("node",$node);
            $this->assign("role",$role);
            $this->display();
        }
    }

    /**
     * 数据验证
     */
    public function ajaxCheckData(){
        // 验证场景
        $scene = I( 'get.scene' );
        // 验证数据
        $data = I( 'post.' );
        switch($scene){
            case 'role' :
                $Role = D( 'Role' );
                // error:0:没有错误,1:有错误 errInfo:有错时为错误信息(数组格式),否则为空, verify:验证通过置为ok,否则置为空
                if(!$Role->create( $data ,'add' )){
                    // 验证不通过
                    $reData = array(
                        'error' => 1,
                        'errInfo' => $Role->getError(),
                    );
                }else{
                    // 验证通过
                    session('checkDataKey',$this->createCheckDataKey($data));
                    $reData = array(
                        'error' => 0,
                        'errInfo' => '',
                    );
                }
                break;
            case 'edit':
                $Role = D( 'Role' );
                $Role->id=$data['id'];
                // error:0:没有错误,1:有错误 errInfo:有错时为错误信息(数组格式),否则为空, verify:验证通过置为ok,否则置为空
                if(!$Role->create( $data ,'edit' )){
                    // 验证不通过
                    $reData = array(
                        'error' => 1,
                        'errInfo' => $Role->getError(),
                    );
                }else{
                    // 验证通过
                    session('checkDataKey',$this->createCheckDataKey($data));
                    $reData = array(
                        'error' => 0,
                        'errInfo' => '',
                    );
                }
                break;
            default	:
                $reData = array(
                    'error' => 1,
                    'errInfo' => '',
                );
                break;
        }
        $this->ajaxReturn( $reData );
    }

}